

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.services.msgbroker &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>cup.services.msgbroker</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.services.msgbroker</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    Msg Broker Service. Every component of a process can produce_msg.</span>

<span class="sd">    This msg broker feature is still exprimental. Do not use it in production</span>
<span class="sd">    until this comment is deleted.</span>
<span class="sd">&quot;&quot;&quot;</span>


<span class="n">MSG_ERROR_DISK_ERROR</span> <span class="o">=</span> <span class="mi">1</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;BrokerCenter&#39;</span><span class="p">,</span> <span class="s1">&#39;SystemErrmsgBroker&#39;</span><span class="p">]</span>


<span class="n">MSG_TYPE_FATAL</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">MSG_TYPE_WARN</span> <span class="o">=</span> <span class="mi">1</span>


<span class="k">class</span> <span class="nc">BaseBroker</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base Broker for a system</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_name</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">name</span>


<div class="viewcode-block" id="BrokerCenter"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.BrokerCenter">[docs]</a><span class="k">class</span> <span class="nc">BrokerCenter</span><span class="p">(</span><span class="n">BaseBroker</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Errmsg broker center</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">BaseBroker</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

<div class="viewcode-block" id="BrokerCenter.produce_msg"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.BrokerCenter.produce_msg">[docs]</a>    <span class="k">def</span> <span class="nf">produce_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">,</span> <span class="n">extra_info</span><span class="p">,</span> <span class="n">error</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;register msg&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="BrokerCenter.comsume_msg"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.BrokerCenter.comsume_msg">[docs]</a>    <span class="k">def</span> <span class="nf">comsume_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg_type from the broker center</span>
<span class="sd">        &quot;&quot;&quot;</span></div></div>


<div class="viewcode-block" id="SystemErrmsgBroker"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker">[docs]</a><span class="k">class</span> <span class="nc">SystemErrmsgBroker</span><span class="p">(</span><span class="n">BrokerCenter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    system errmsg broker, you can use it to determine whether</span>
<span class="sd">    exiting from the system is on the way</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">BrokerCenter</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

<div class="viewcode-block" id="SystemErrmsgBroker.need_stop"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.need_stop">[docs]</a>    <span class="k">def</span> <span class="nf">need_stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return True if the system registered on</span>
<span class="sd">            the path needs to stop immediately</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.fatal_alert"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.fatal_alert">[docs]</a>    <span class="k">def</span> <span class="nf">fatal_alert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">need_stop</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;fatal alert systems&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.warnning_alert"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.warnning_alert">[docs]</a>    <span class="k">def</span> <span class="nf">warnning_alert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        warnning alert</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.register_msg"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.register_msg">[docs]</a>    <span class="k">def</span> <span class="nf">register_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msgtype</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;register msg into the system&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.get_fatal_alerts"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.get_fatal_alerts">[docs]</a>    <span class="k">def</span> <span class="nf">get_fatal_alerts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get fatal alerts of the current running round</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.clean_data"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.clean_data">[docs]</a>    <span class="k">def</span> <span class="nf">clean_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">exclude_msgtypes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        clean data of the remaining data</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.register_wakeup"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.register_wakeup">[docs]</a>    <span class="k">def</span> <span class="nf">register_wakeup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msgtype</span><span class="p">,</span> <span class="n">alert_cap_num</span><span class="p">,</span> <span class="n">callfunc</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register wakeups.</span>

<span class="sd">        :param alert_cap_num:</span>
<span class="sd">            If alert_cap_num is 0, whenever a msg of msgtype is received,</span>
<span class="sd">            the callfunc will be called.</span>
<span class="sd">        :param msgtype:</span>
<span class="sd">            [msgbroker.FATAL|msgbroker.WARN]</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

    <span class="k">def</span> <span class="nf">_wakeup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msgtype</span><span class="p">,</span> <span class="n">alert_cap_num</span><span class="p">,</span> <span class="n">callfunc</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        wake up callfunc</span>
<span class="sd">        &quot;&quot;&quot;</span>

<div class="viewcode-block" id="SystemErrmsgBroker.register_msgtype_callback"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.register_msgtype_callback">[docs]</a>    <span class="k">def</span> <span class="nf">register_msgtype_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">,</span> <span class="n">callback_func</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register msgtype with callback functions</span>
<span class="sd">        &quot;&quot;&quot;</span></div></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>